package com.sltx.util;
import net.sf.json.JSONException;
import net.sf.json.JSONObject;
import org.apache.commons.lang.time.DateUtils;

import java.io.IOException;
import java.io.InputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * User: wangxianzhao
 * Date: 15-12-21
 * Time: 下午2:14
 * To change this template use File | Settings | File Templates.
 */
public class CommonUtils {

    /**
     * JsonObject 转map
     * @throws JSONException
     * */
    public static Map parserToMap(JSONObject json) throws JSONException {
        Map map=new HashMap();
//	    JSONObject json=JSONObject.fromObject(s);
        Iterator keys=json.keys();
        while(keys.hasNext()){
            String key=(String) keys.next();
            String value=json.get(key).toString();
//	        if(value.startsWith("{")&&value.endsWith("}")){
//	            map.put(key, parserToMap(value));
//	        }else{
//	            map.put(key, value);
//	        }
            map.put(key, value);
        }
        return map;
    }

    public static String getOsType(){
		String type=System.getProperty("os.name");
		if(type.startsWith("win")||type.startsWith("Win")){
			return "windows";
		}else{
			return "linux";
		}
	}
	/**
	 * 获取properties文件值
	 * @param fileName 配置文件名称
	 * @param key　
	 * @return
	 */
	
	public static String getProperty(String fileName,String key) {
		Properties properties = new Properties();
        //            InputStreamReader ipr = new InputStreamReader(CommonUtils.class.getClassLoader()
//                    .getResourceAsStream(fileName),"GBK");
        InputStream inputStream = CommonUtils.class.getClassLoader()
                .getResourceAsStream(fileName);
        if (key != null) {
            try {
                properties.load(inputStream);
            } catch (IOException e) {
                e.printStackTrace();
            }
            return properties.getProperty(key);
        }

        return null;
	}

    /**
     *  获取上周一
     * @param date
     * @return
     */
    public static Date getLastWeekMonday(Date date) {
        Date a = DateUtils.addDays(date, -1);
        Calendar cal = Calendar.getInstance();
        cal.setTime(a);
        cal.add(Calendar.WEEK_OF_YEAR, -1);// 一周
        cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        return cal.getTime();
    }

    /**
     *  获取上周日
     * @param date
     * @return
     */
    public static Date  getLastWeekSunday(Date date) {
        Date a = DateUtils.addDays(date, -1);
        Calendar cal = Calendar.getInstance();
        cal.setTime(a);
        cal.set(Calendar.DAY_OF_WEEK, 1);
        return cal.getTime();
    }

    /**
     * 日期转换成字符串              yyyy-MM-dd HH:mm:ss
     * @param date
     * @return str
     */
    public static String DateToStr(Date date) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String str = format.format(date);
        return str;
    }

    /**
     *  日期转换成字符串     yyyyMMdd
     * @param date
     * @return
     */
    public static String DateToStr_yyyyMMdd(Date date) {
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
        String str = format.format(date);
        return str;
    }

    /**
     * 字符串转换成日期
     * @param str
     * @return date
     */
    public static Date StrToDate(String str) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = null;
        try {
            date = format.parse(str);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }
}
